package com.dungnt.model;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.dungnt.entity.AlarmTime;
import com.dungnt.vtime.alarm.AlarmActionAlgorithm;
import com.dungnt.vtime.common.interfaces.IActionAlgorithm;
import com.dungnt.vtime.utils.Day;
import com.dungnt.vtime.utils.Utils;


public class Alarm extends AbstractModel {
	
	public static final String TABLE_NAME = "alarm";
	public static final String COL_ID = AbstractModel.COL_ID;
	public static final String COL_HOUR = "hour";
	public static final String COL_MINUTE = "minute";
	public static final String COL_TYPE = "type";
	public static final String COL_RINGTONE = "tone";
	public static final String COL_MESSAGE = "message";

	// DAY OF WEEK
	public static final String COL_SUNDAY = "sunday";
	public static final String COL_MONDAY = "monday";
	public static final String COL_TUESDAY = "tuesday";
	public static final String COL_WEDNESDAY = "wednesday";
	public static final String COL_THURSDAY = "thursday";
	public static final String COL_FRIDAY = "friday";
	public static final String COL_SATURDAY = "saturday";
	
	static String getSql() {
		return Utils.concat("CREATE TABLE ", TABLE_NAME, " (",
				AbstractModel.getSql(),
				COL_HOUR, " INTEGER, ",
				COL_MINUTE, " INTEGER, ",			
				COL_TYPE, " INTEGER, ",
				COL_RINGTONE, " INTEGER, ",
				COL_MESSAGE, " TEXT, ",
				COL_SUNDAY, " INTEGER, ",
				COL_MONDAY, " INTEGER, ",
				COL_TUESDAY, " INTEGER, ",
				COL_WEDNESDAY, " INTEGER, ",
				COL_THURSDAY, " INTEGER, ",
				COL_FRIDAY, " INTEGER, ",
				COL_SATURDAY, " INTEGER",
				");");
	}
	
	long save(SQLiteDatabase db) {
		ContentValues cv = new ContentValues();

		cv.put(COL_HOUR, alarmTime.getHour());
		cv.put(COL_MINUTE, alarmTime.getMinute());
		cv.put(COL_TYPE, alarmTime.getType());
		cv.put(COL_RINGTONE, alarmTime.getRingtone());
		cv.put(COL_MESSAGE, alarmTime.getMessage());
		cv.put(COL_SUNDAY, 0);
		cv.put(COL_MONDAY, 0);
		cv.put(COL_TUESDAY, 0);
		cv.put(COL_WEDNESDAY, 0);
		cv.put(COL_THURSDAY, 0);
		cv.put(COL_FRIDAY, 0);
		cv.put(COL_SATURDAY, 0);
		for (Day item : alarmTime.getFrequently()) {
			switch (item) {
			case SUNDAY:
				cv.put(COL_SUNDAY, 1);
				break;
			case MONDAY:
				cv.put(COL_MONDAY, 1);
				break;
			case TUESDAY:
				cv.put(COL_TUESDAY, 1);
				break;
			case WEDNESDAY:
				cv.put(COL_WEDNESDAY, 1);
				break;
			case THURSDAY:
				cv.put(COL_THURSDAY, 1);
				break;
			case FRIDAY:
				cv.put(COL_FRIDAY, 1);
				break;
			case SATURDAY:
				cv.put(COL_SATURDAY, 1);
				break;
			default:
				break;
			}
		}

		long tmp = db.insert(TABLE_NAME, null, cv); 
		
		return tmp;
	}
	
	boolean update(SQLiteDatabase db) {
		ContentValues cv = new ContentValues();
		super.update(cv);
		cv.put(COL_HOUR, alarmTime.getHour());
		cv.put(COL_MINUTE, alarmTime.getMinute());
		cv.put(COL_TYPE, alarmTime.getType());
		cv.put(COL_RINGTONE, alarmTime.getRingtone());
		cv.put(COL_MESSAGE, alarmTime.getMessage());
		cv.put(COL_SUNDAY, 0);
		cv.put(COL_MONDAY, 0);
		cv.put(COL_TUESDAY, 0);
		cv.put(COL_WEDNESDAY, 0);
		cv.put(COL_THURSDAY, 0);
		cv.put(COL_FRIDAY, 0);
		cv.put(COL_SATURDAY, 0);
		for (Day item : alarmTime.getFrequently()) {
			switch (item) {
			case SUNDAY:
				cv.put(COL_SUNDAY, 1);
				break;
			case MONDAY:
				cv.put(COL_MONDAY, 1);
				break;
			case TUESDAY:
				cv.put(COL_TUESDAY, 1);
				break;
			case WEDNESDAY:
				cv.put(COL_WEDNESDAY, 1);
				break;
			case THURSDAY:
				cv.put(COL_THURSDAY, 1);
				break;
			case FRIDAY:
				cv.put(COL_FRIDAY, 1);
				break;
			case SATURDAY:
				cv.put(COL_SATURDAY, 1);
				break;
			default:
				break;
			}
		}
		return db.update(TABLE_NAME, cv, COL_ID+" = ?", new String[]{String.valueOf(id)}) 
				== 1 ? true : false;
	}
	
	public boolean load(SQLiteDatabase db) {
		Cursor cursor = db.query(TABLE_NAME, null, COL_ID+" = ?", new String[]{String.valueOf(id)}, null, null, null);
		try {
			if (cursor.moveToFirst()) {
				reset();
				super.load(cursor);
				alarmTime.setHour(cursor.getInt(cursor.getColumnIndex(COL_HOUR)));
				Log.d("DungNT", "Hour: " + alarmTime.getHour());
				alarmTime.setMinute(cursor.getInt(cursor.getColumnIndex(COL_MINUTE)));
				Log.d("DungNT", "Minute: " + alarmTime.getMinute());
				alarmTime.setType(cursor.getInt(cursor.getColumnIndex(COL_TYPE)));
				alarmTime.setMessage(cursor.getString(cursor.getColumnIndex(COL_MESSAGE)));
				ArrayList<Day> list = new ArrayList<Day>();
				if (cursor.getInt(cursor.getColumnIndex(COL_SUNDAY)) > 0) 
					list.add(Day.SUNDAY);
				if (cursor.getInt(cursor.getColumnIndex(COL_MONDAY)) > 0) 
					list.add(Day.MONDAY);
				if (cursor.getInt(cursor.getColumnIndex(COL_TUESDAY)) > 0) 
					list.add(Day.TUESDAY);
				if (cursor.getInt(cursor.getColumnIndex(COL_WEDNESDAY)) > 0) 
					list.add(Day.WEDNESDAY);
				if (cursor.getInt(cursor.getColumnIndex(COL_THURSDAY)) > 0) 
					list.add(Day.THURSDAY);
				if (cursor.getInt(cursor.getColumnIndex(COL_FRIDAY)) > 0) 
					list.add(Day.FRIDAY);
				if (cursor.getInt(cursor.getColumnIndex(COL_SATURDAY)) > 0) 
					list.add(Day.SATURDAY);
				alarmTime.setFrequently(list);
				
				return true;
			}
			return false;
		} finally {
			cursor.close();
		}
	}
	
	public static Cursor list(SQLiteDatabase db) {
		String[] columns = {COL_ID, COL_HOUR, COL_MINUTE};
		
		return db.query(TABLE_NAME, columns, null, null, null, null, null);
	}
	
	public boolean delete(SQLiteDatabase db) {
		boolean status = false;
		String[] whereArgs = new String[]{String.valueOf(id)};

		db.beginTransaction();
        try {
			db.delete(TABLE_NAME, COL_ID+" = ?", whereArgs);
			status = db.delete(TABLE_NAME, COL_ID+" = ?", whereArgs)
					== 1 ? true : false;
	        db.setTransactionSuccessful();
	    } catch (Exception e) {
	    } finally {
	    	db.endTransaction();
	    }
		return status;		
	}	
	
	//--------------------------------------------------------------------------

	private AlarmTime alarmTime = new AlarmTime();

	public void reset() {
		super.reset();
		alarmTime.setHour(0);
		alarmTime.setMinute(0);
		alarmTime.setType(AlarmTime.ALARM_TYPE_ONCE);
		alarmTime.setMessage("");
		alarmTime.setFrequently(new ArrayList<Day>());
	}

	public Alarm() {
		setActionAlgorithm(new AlarmActionAlgorithm(this));
	}

	public Alarm(long id) {
		this.id = id;
		setActionAlgorithm(new AlarmActionAlgorithm(this));
	}
/*	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if ((obj == null) || (obj.getClass() != this.getClass()))
			return false;
		
		return id == ((Alarm)obj).id;
	}
 
	@Override
	public int hashCode() {
		return 1;
	}	
*/

	public Alarm(AlarmTime alarmTime) {
		super();
		this.alarmTime = alarmTime;
		setActionAlgorithm(new AlarmActionAlgorithm(this));
	}

	public AlarmTime getAlarmTime() {
		return alarmTime;
	}

	public void setAlarmTime(AlarmTime alarmTime) {
		this.alarmTime = alarmTime;
	}

	///////////////////////////////////////////////
	//---------------- Define action when time come
	///////////////////////////////////////////////
	
	public IActionAlgorithm action;

	public void doOnTime(Context context) {
		action.doOnTime(context);
	}
	
	public void setActionAlgorithm(IActionAlgorithm algorithm) {
		action = algorithm;
	}
}
